home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / midi / misc / Midi2TeX / src / tp_heap1.pas < prev    next >
Pascal/Delphi Source File  |  1992-04-02  |  9KB  |  352 lines

  1. UNIT TP_Heap1;
  2.  
  3. INTERFACE
  4. uses TP_DECL,
  5. {$IFDEF PC}
  6.      Crt,
  7. {$ENDIF}
  8.      TP_DEBUG;
  9.  
  10. Procedure Append(VAR ThisList: HeapRecord; N: NoteRecPoint);
  11. Procedure InitNotePool;
  12. Procedure KillNotePool;
  13. Procedure KillNoteLists;
  14. Procedure KillList(VAR ThisList : HeapRecord);
  15. Function  Empty(ThisList:HeapRecord):Boolean;
  16. Procedure InsertOnTop(VAR ThisList: HeapRecord; N : NoteRecPoint);
  17. Procedure Insert(VAR ThisList: HeapRecord; Nin, N: NoteRecPoint);
  18. Procedure Remove(VAR ThisList : HeapRecord; N : NoteRecPoint);
  19. Procedure FirstNote(ThisList:HeapRecord; VAR N:NoteRecPoint);
  20. Procedure LastNote(ThisList:HeapRecord; VAR N : NoteRecPoint);
  21. Procedure NextNote(N:NoteRecPoint; VAR P: NoteRecPoint);
  22. Procedure PrevNote(N:NoteRecPoint; VAR P : NoteRecPoint);
  23. Function  GetFreeNote : NoteRecPoint;
  24. Procedure ResetNoteRec(N : NoteRecPoint);
  25. Procedure BringFreeNote(N:NoteRecPoint);
  26. Function  EqualsNote(N:NoteRecPoint;ThisNote : Byte) : Boolean;
  27. Procedure Exchange(VAR ThisList : HeapRecord;
  28.                    VAR N1, N2 : NoteRecPoint);
  29. Function NoteList2String(ThisList : HeapRecord):String;
  30. Function ChordNoteList2String(ThisList : HeapRecord):String;
  31. IMPLEMENTATION
  32.  
  33. (**********************************************************)
  34.    Function Empty(ThisList:HeapRecord):Boolean;
  35. (**********************************************************)
  36. Begin
  37. (* Empty:=ThisList.Tail=nil; *) (* does not work properly yet, FIX !! *)
  38. Empty:=ThisList.Size=0; 
  39. End;
  40.  
  41. (********************************************************************)
  42.    Procedure InsertOnTop(VAR ThisList: HeapRecord; N : NoteRecPoint);
  43. (********************************************************************)
  44. Begin
  45. With ThisList Do
  46.    Begin
  47.    if Tail=nil then
  48.       Begin
  49.       Tail:=N; N^.Next:=N;  N^.Prev:=N;
  50.       end
  51.    else
  52.       Begin
  53.       N^.Next:=Tail^.Next; N^.Prev:=Tail;
  54.       Tail^.Next^.Prev:=N;
  55.       Tail^.Next:=N;
  56.       End;
  57.    Inc(Size);
  58.    End;
  59. End;
  60.  
  61. (**********************************************************)
  62.  Procedure Append(VAR ThisList:HeapRecord; N: NoteRecPoint);
  63. (**********************************************************)
  64. begin
  65. with ThisList do
  66.   Begin
  67.   If Tail=nil Then
  68.     Begin
  69.     Tail:=N;  N^.Next:=N;  N^.Prev:=N;
  70.     End
  71.   Else
  72.     Begin
  73.     N^.Prev:=Tail;
  74.     N^.Next:=Tail^.Next;
  75.     Tail^.Next^.Prev:=N;
  76.     Tail^.Next:=N;
  77.     Tail:=N;
  78.     End;
  79.   Inc(Size);
  80.   End;
  81. end;
  82.  
  83. (*****************************************************************)
  84.  Procedure Insert(VAR ThisList: HeapRecord; Nin, N: NoteRecPoint);
  85. (* inserts N BEFORE Nin !!                                       *)
  86. (*****************************************************************)
  87. begin
  88. With ThisList Do
  89.   Begin
  90.   if (Tail = nil) then  (* NoteList is empty *)
  91.     InsertOnTop(ThisList,N)
  92.   else
  93.     if Nin=Tail^.Next Then    (* N should be inserted in front of the first item in list ...*)
  94.        (* Append(ThisList,N) *)
  95.        InsertOnTop(ThisList,N)
  96.     else
  97.        Begin
  98.        Nin^.Prev^.Next:=N;
  99.        N^.Prev:=Nin^.Prev;
  100.        Nin^.Prev:=N;
  101.        N^.Next:=Nin;
  102.        Inc(Size);
  103.        End;
  104.   End; (* with *)
  105. end;
  106.  
  107. (**************************************************************)
  108.   Procedure Remove(VAR ThisList : HeapRecord; N : NoteRecPoint);
  109. (**************************************************************)
  110. var
  111.   P: NoteRecPoint;
  112. begin
  113. with ThisList Do
  114.   Begin
  115.   if Tail <> nil then
  116.    begin
  117.     P := Tail; (* pointer to Tail *)
  118.     while (P^.Next<>N) and (P^.Next <> Tail) do P := P^.Next;
  119.     if P^.Next = N then
  120.       begin
  121.         P^.Next := N^.Next;
  122.         N^.Next^.Prev:=P;
  123.         if Tail= N then if P = N then Tail:= nil else Tail:= N^.Prev;
  124.       end; (* if *)
  125.    end; (* if *)
  126.   Dec(Size);
  127.   end; (* with *)
  128. end;
  129.  
  130. (*****************************************************)
  131.   Procedure FirstNote(ThisList:HeapRecord; VAR N:NoteRecPoint);
  132. (*****************************************************)
  133. VAR c : CHAR;
  134. Begin
  135. N:=ThisList.Tail^.Next;
  136. If KeyPressed Then
  137.    Begin   
  138.    c:=ReadKey;
  139.    if c='q' then ErrorExit(17);
  140.    End;
  141. End;
  142.  
  143. (*****************************************************)
  144.  Procedure LastNote(ThisList:HeapRecord; VAR N : NoteRecPoint);
  145. (*****************************************************)
  146. VAR  c : CHAR;
  147. Begin
  148. N:=ThisList.Tail;
  149. If KeyPressed Then
  150.    Begin   
  151.    c:=ReadKey;
  152.    if c='q' then ErrorExit(17);
  153.    End;
  154. End;
  155.  
  156. (*****************************************************)
  157.  Procedure NextNote(N:NoteRecPoint; VAR P: NoteRecPoint);
  158. (*****************************************************)
  159. VAR c : CHAR;
  160. Begin
  161. P:=N^.Next;
  162. If KeyPressed Then
  163.    Begin   
  164.    c:=ReadKey;
  165.    if c='q' then ErrorExit(17);
  166.    End;
  167. End;
  168.  
  169. (*****************************************************)
  170.  Procedure PrevNote(N:NoteRecPoint; VAR P : NoteRecPoint);
  171. (*****************************************************)
  172. Begin
  173.  P:=N^.Prev;
  174. End;
  175.  
  176. (***************************************************)
  177.     Procedure ResetNoteRec(N : NoteRecPoint);
  178. (***************************************************)
  179.  Begin
  180.  FillChar(N^,SizeOf(NoteRecord),0);
  181.  End;
  182.  
  183.  
  184. (*****************************************************)
  185.  Function GetFreeNote : NoteRecPoint;
  186. (*****************************************************)
  187. VAR N : NoteRecPoint;
  188. Begin
  189. With NotePool Do
  190.   If Size>0 then
  191.      Begin N:=Tail; Remove(NotePool,N); ResetNoteRec(N); end
  192.   else
  193.      ErrorExit(10);
  194. GetFreeNote:=N;
  195. End;
  196.  
  197. (*****************************************************)
  198.  Procedure BringFreeNote(N:NoteRecPoint);
  199. (*****************************************************)
  200. Begin
  201. Append(NotePool,N);
  202. End;
  203.  
  204. (*************************************************************)
  205.  Function EqualsNote(N:NoteRecPoint;ThisNote : Byte) : Boolean;
  206. (*************************************************************)
  207. Begin
  208. If N^.NoteVal=ThisNote then
  209.   EqualsNote:=TRUE
  210. else
  211.   EqualsNote:=FALSE;
  212. End;
  213.  
  214.  
  215. (*************************)
  216.   Procedure InitNotePool;
  217. (*************************)
  218. CONST POOLSIZE=200;
  219.  
  220. VAR
  221.     N  :  NoteRecPoint;
  222.  
  223. Begin
  224.   for i:=1 to POOLSIZE do
  225.      begin
  226.      If MaxAvail>SizeOf(NoteRecord) Then
  227.          GetMem(N,SizeOf(NoteRecord))
  228.      Else ErrorExit(9);
  229.      Append(NotePool,N);
  230.      end;
  231. End; (* InitNoteHeap *)
  232.  
  233. (*************************)
  234.   Procedure KillNotePool;
  235. (*************************)
  236. Begin
  237. KillList(NotePool);
  238. End;
  239.  
  240. (*************************)
  241.   Procedure KillNoteLists;
  242. (*************************)
  243. VAR I : Integer;
  244. Begin
  245. for i:=1to 16 do
  246.   with TrackArray[i] do
  247.      if NoteList.Tail<>NIL Then KillList(NoteList);
  248. KillList(NotePool);
  249. End;
  250.  
  251.  
  252. (***********************************************)
  253.   Procedure KillList(VAR ThisList : HeapRecord);
  254. (***********************************************)
  255. VAR
  256.     N,P :  NoteRecPoint;
  257. Begin
  258. LastNote(ThisList,P);
  259. While NOT Empty(ThisList) do
  260.      begin
  261.      N:=P^.Next;
  262.      Remove(ThisList,N);
  263.      FreeMem(N,SizeOf(NoteRecord));
  264.      end;
  265. End; (* InitNoteHeap *)
  266.  
  267.  
  268. (***********************************************************)
  269.     Function NoteList2String(ThisList : HeapRecord):String;
  270. (***********************************************************)
  271. Var Tmpstr,tmp : String;
  272.     N,P        : NoteRecPoint;
  273. Begin
  274. N:=ThisList.Tail^.Next;
  275. P:=N;
  276. Tmpstr:='';
  277. Repeat
  278.    Case N^.Event OF
  279.         NOTEON,NOTEOFF : Begin
  280.                          Str(N^.NoteVal,tmp);
  281.                          TmpStr:=Tmpstr+tmp+' ';
  282.                          End;
  283.         Else TmpStr:=Tmpstr+'n ';
  284.         End;
  285.    NextNote(N,N)
  286. until N=P;
  287. NoteList2String:=TmpStr;
  288. End;
  289.  
  290. (***********************************************************)
  291.     Function ChordNoteList2String(ThisList : HeapRecord):String;
  292. (***********************************************************)
  293. Var Tmpstr,tmp : String;
  294.     N,P        : NoteRecPoint;
  295. Begin
  296. N:=ThisList.Tail^.Next;
  297. P:=N;
  298. Tmpstr:='';
  299. Repeat
  300.    Case N^.Event OF
  301.         NOTEON,NOTEOFF : Begin
  302.                          Str(N^.NoteVal,tmp);
  303.                          If N^.ChordNote Then
  304.                             TmpStr:=Tmpstr+'-- '
  305.                          Else
  306.                             TmpStr:=Tmpstr+tmp+' ';
  307.                          End;
  308.         Else TmpStr:=Tmpstr+'n ';
  309.         End;
  310.    NextNote(N,N)
  311. until N=P;
  312. ChordNoteList2String:=TmpStr;
  313. End;
  314.  
  315. (*************************************************)
  316.   Procedure Exchange(VAR ThisList : HeapRecord;
  317.                      VAR N1, N2 : NoteRecPoint);
  318. (*************************************************)
  319. Var P1,P2,F : NoteRecPoint;
  320. Begin
  321. F:=ThisList.Tail^.Next; (* first item in notelist *)
  322. NextNote(N1,P1);
  323. NextNote(N2,P2);
  324. If P1=N2 Then
  325.   If P2=N1 Then (* only two notes in the list *)
  326.     With ThisList Do NextNote(Tail,Tail)
  327.   Else
  328.     Begin
  329.     Remove(ThisList,N2);
  330.     (*If N1=F Then Append(ThisList,N2) Else *)
  331.     Insert(ThisList,N1,N2);
  332.     Remove(ThisList,N1);
  333.     If P2=F Then Append(ThisList,N1) Else Insert(ThisList,P2,N1);
  334.     End
  335. Else
  336.    Begin
  337.    Remove(ThisList,N1);
  338.    (* If N2=F Then Append(ThisList,N1) Else *)
  339.    Insert(ThisList,N2,N1);
  340.    Remove(ThisList,N2);
  341.    If P1=F Then Append(ThisList,N2) Else Insert(ThisList,P1,N2);
  342.    End;
  343. End; (* exchange *)
  344.  
  345.  
  346. Begin
  347. NotePool.Tail:=nil;
  348. NotePool.Size:=0;
  349. For i:=1 to 16 do with TrackArray[i].NoteList do
  350.   Begin Size:=0; Tail:=NIL; End;
  351. End.
  352.